<template>
  <div class='container'>
    <!-- 搜索框 -->
    <el-form :inline="true" :model="formInline">
      <el-form-item label="关键字">
        <el-input v-model="formInline.keywords" placeholder="根据文章标题搜索"></el-input>
      </el-form-item>
      <el-form-item label="状态">
        <el-select v-model="formInline.state" placeholder="请选择">
          <el-option label="启用" value="1"></el-option>
          <el-option label="禁用" value="0"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item class="btn">
        <el-button @click="reset">清除</el-button>
        <el-button type="primary" @click="onSubmit">查询</el-button>
        <el-button class="add" type="success" @click="addskill">
          <i class="el-icon-edit"></i>
          新增技巧</el-button>
      </el-form-item>
    </el-form>
    <div class="count">
      <i class="el-icon-info"></i>
      数据一共 {{page.total}} 条
    </div>
  <!-- 列表 -->
  <el-table
      :data="Data"
      style="width: 100%">
      <el-table-column type="index" label="序号" width="80">
      </el-table-column>
      <el-table-column prop="title" label="文章标题" width="300">
        <template v-slot={row}>
           {{row.title}}
           <i @click="checkVideoFun(row.videoURL)" class="el-icon-film purple" v-if="row.videoURL"></i>
           <!-- 外层遮罩 -->
           <div class='mask' v-if='videoState == true' @click='masksCloseFun'></div>
           <!-- //弹窗 -->
          <div class="videomasks" v-if="videoState == true">
          <!-- //视频：h5的视频播放video -->
            <video :src='videoSrc' controls='controls' autoplay>
             您的浏览器不支持 video 标签。
            </video>
          </div>
        </template>

      </el-table-column>
      <el-table-column prop="visits" label="阅读数" width="100">
      </el-table-column>
      <el-table-column prop="username" label="录入人" width="100">
      </el-table-column>
      <el-table-column prop="createTime" label="录入时间" width="160">
        <template v-slot={row}>
          {{ Time(row.createTime) }}
        </template>
      </el-table-column>
      <el-table-column label="状态" width="100">
        <template v-slot={row}>
          {{ Newstate(row.state) }}
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template v-slot={row}>
          <el-button type="text" @click="views(row)">预览</el-button>
          <el-button type="text" @click="isState(row)"> {{row.state === 0 ? '启用' : '禁用'}}</el-button>
          <el-button type="text" @click="edit(row)" :disabled = "row.state === 1" >修改</el-button>
          <el-button type="text" @click="del(row)" :disabled = "row.state === 1" >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
     <el-pagination
     style="text-align:right; margin-top:20px"
      @next-click = next(1)
      @prev-click = prev(-1)
      :current-page="+page.page"
      :page-sizes="[10, 20, 30, 40]"
      :page-size="10"
      layout="prev, pager, next,sizes, jumper"
      :total="page.total">
    </el-pagination>
    <!-- 预览 -->
    <Preview :PVisible.sync ="PVisible" :Pdata="Pdata"/>
    <!-- 新增和修改 -->
    <articlesAdd :visible.sync = "visible" :title="drawerType" @addSubmit ="addSubmit" :form.sync ="row"/>
  </div>
</template>

<script>
import dayjs from 'dayjs'
import { list, remove, detail, add, update } from '@/api/hmmm/articles'
import Preview from '../components/articles-preview.vue'
import articlesAdd from '../components/articles-add.vue'
export default {
  components: {
    Preview,
    articlesAdd
  },
  data () {
    return {
      visible: false,
      PVisible: false,
      drawerType: '新增文章',
      videoSrc: '',
      videoState: false,
      row: {},
      page: {
        page: 1,
        pagesize: 10,
        total: 0,
        pages: 0 // 总页数
      },
      formInline: {
        keywords: '',
        status: ''
      },
      Data: [],
      Pdata: {},
      tableData: {
        title: '', // 标题
        visits: '', // 阅读人数
        username: '', // 录入人
        createTime: '', // 录入时间
        state: 0// 状态
      }
    }
  },
  created () {
    this.getList()
  },
  methods: {
    // 获取列表
    async getList () {
      const { data } = await list(this.page)
      this.Data = data.items
      this.page.total = data.counts
      this.page.page = data.page
      this.page.pagesize = data.pagesize
      this.page.pages = data.pages
      console.log(data)
    },
    async onSubmit () {
      // console.log(this.formInline)
      const res = await list({
        keyword: this.formInline.keywords,
        state: this.formInline.state,
        page: this.page.page,
        pagesize: this.page.pagesize
      })
      this.Data = res.data.items
    },
    reset () {
      this.formInline = {
        keywords: '',
        status: ''
      }
      this.getList()
    },
    addskill () {
      this.visible = true
    },
    async addSubmit (form) {
      try {
        if (this.drawerType === '添加文章') {
          await add(form)
          this.$message.success('添加文章成功')
        }
        if (this.drawerType === '修改文章') {
          await update(form)
          this.$message.success('修改文章成功')
        }
        this.visible = false
        await this.getList()
      } catch (e) {
        console.log(e)
      }
    },
    next () {
      if (this.page.page > this.page.pages) return
      this.page.page++
      this.getList()
    },
    prev () {
      if (this.page.page === 1) return
      this.page.page--
      this.getList()
    },
    Newstate (state) {
      let Cstate = '已启用'
      if (state === 1) Cstate = '已启用'
      if (state === 0) Cstate = '已禁用'
      return Cstate
    },
    Time (data) {
      return dayjs(data).format('YYYY-MM-DD HH:mm:ss')
    },
    async views (row) {
      // console.log(row)
      const res = await detail(row)
      console.log(res)
      this.Pdata = res.data
      this.PVisible = true
    },
    isState (row) {
      if (row.state === 1) {
        row.state = 0
        this.$message.success('操作成功')
      } else {
        row.state = 1
        this.$message.success('操作成功')
      }
    },
    async edit (row) {
      this.visible = true
      this.drawerType = '修改文章'
      this.row = row
      // const res = await update(row)
      // console.log(res)
    },
    async del (row) {
      await this.$confirm('此操作将永久删除该文章, 是否继续?', '提示', { type: 'warning' })
      await remove(row)
      this.getList()
      this.$message.success('删除成功')
    },
    checkVideoFun (videos) {
      this.videoState = true
      this.videoSrc = videos
    },
    masksCloseFun () {
      this.videoState = false
    }
  }
}
</script>

<style scoped lang='less'>
.container {
  margin: 5px 10px 0 10px;
  padding: 20px;
  background-color: #fff;
  .count {
    margin-bottom: 15px;
    background-color: #f4f4f5;
    color: #909399;
    width: 100%;
    padding: 8px 16px;
    border-radius: 4px;
    align-items: center;
  }
  [class^=el-icon-film] {
    text-align: center;
    color: #0000ff;
    font-size: 20px;
  }
  .btn {
    .add {
      margin-left: 350px;
    }
  }
::v-deep .el-dialog__header {
  // background: #fff;
  .el-dialog__title {
  color: #333;
  }
  .el-dialog__close {
  color: #333;
  }
  .el-icon-warning-outline {
    font-size: 20px !important;
  }
}
.el-dialog__body {
  color: #333;
  font-size: 14px;
}
}
.mask{
    position:fixed;
    top:0;
    left:0;
    bottom:0;
    right:0;
    z-index:10;
    background-color: #fff;
    opacity: .1;
}
.videomasks{
    min-width: 900px;
    position: fixed;
    left: 50%;
    top: 50%;
    z-index: 20;
    transform: translate(-50%,-50%);
  }
  .videomasks video{
    width: 100%;
    height: 100%;
  }
</style>
